// TianguoguoXiaopu-master/pages/sys/bannerManage/checkbox_goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommendGoods:[],
    recommendGoodsShow:[],
    isEnd: false, //到底啦
    checkNum:0,//选择数
    checktype:""//选择类型 商品/活动页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '选择参加活动的商品',
    })
     this.setData({
       checktype:options.leixing
     })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoods()
  },

  change(e) {
   
    
    let items = this.data[`recommendGoodsShow`];
    items.forEach(item => {
      if(item._id === e.detail.key) {
        item.checked = e.detail.checked;
      }
    });
    this.setData({
      [`recommendGoodsShow`]: items,
     
    });
   let nnn =  0;//that.data.checkNum
  
    for (let index = 0; index < this.data.recommendGoodsShow.length; index++) {
      const element = this.data.recommendGoodsShow[index];
      if(element.checked){
        nnn+=1;
      }
    }
    this.setData({
      checkNum: nnn,
     
    });
  },
  //确定选择
  queding(){
    let checkArray =[]
    for (let index = 0; index < this.data.recommendGoodsShow.length; index++) {
      const element = this.data.recommendGoodsShow[index];
      if(element.checked){
        checkArray.push(element)
      }
    }
    if(this.data.checktype==1 && checkArray.length>1){
      wx.showModal({
        title: '提示',
        content: '已选跳转类型为商品详情，所以只能选一个商品!或者请返回前一页修改类型',
      })
      
      return
    }
        //获取页面栈
    let pages = getCurrentPages();
    //获取所需页面
    let prevPage = pages[pages.length -2];//上一页
    prevPage.setData({
        checkedGoods:checkArray,//你需要传过去的数据
    });
    wx.navigateBack({
      delta: 1,
    })
  },
  getGoods: function () {
    var that = this;
    
     that.setData({
      loadingStatus: true
    })
    
    wx.cloud.callFunction({
      // 云函数名称
      name: 'crud',
      // 传给云函数的参数
      data: {
        db:"goods",
        type:"getall",
        data:"",
        indexKey:"",
        //待筛选的参数-库存 >0
        commandtype:[{
          type:'gt',
          ziduan:'stores',
          value:0
        }],
        condition:{
          status:1,
         
        },
      },
    })
    .then(res => {
    
      that.setData({
        goods: [],
      });
      var goods = [];
    
      if(res.result){
        if ( res.result.data.length == 0) {
          that.setData({
            prePageBtn: false,
            nextPageBtn: true,
            toBottom: true
          });
          return;
        }
        let result =  res.result.data
        for (var i = 0; i < result.length; i++) {
          goods.push(result[i]);
        } 
      
       
        //  var page = that.data.page;
        // var pageSize = that.data.pageSize;
        /*
        //评价几颗星
        for (let i = 0; i < goods.length; i++) {
          goods[i].starscore = (goods[i].numberGoodReputation / goods[i].numberOrders) * 5
          goods[i].starscore = Math.ceil(goods[i].starscore / 0.5) * 0.5
          goods[i].starpic = starscore.picStr(goods[i].starscore)
        }*/
        that.setData({
          recommendGoods: goods,
        });
   
        that.setData({
          loadingStatus: false,
          loadingFinish: true
        })
        
        that.getRGshow()
        setTimeout(() => {
          that.setData({
            loadingFinish: false
          })
        }, 1500)
    
        }
    })
    .catch(console.error)
  },
  
   //精品推荐分页展示
   getRGshow: function () {
    
    console.log("加载下页")
    var that = this;
    if (that.data.isEnd) {
      console.log('isEnd, return...')
      return;
    }
    console.log('notEnd, continue')
    that.setData({
      loadingMore: true
    })
    var pageSize = 99999999; //一次加载万所有的推荐商品，避免minui显示问题that.data.recommendGoods.length
    var recommendGoodsShow = that.data.recommendGoodsShow;
    var rgShowLen = recommendGoodsShow.length;
  
   
      if (rgShowLen + pageSize <= that.data.recommendGoods.length) {
        for (var i = rgShowLen; i < rgShowLen + pageSize; i++) {
          recommendGoodsShow.push(that.data.recommendGoods[i])
       }

      
      that.setData({
        recommendGoodsShow: recommendGoodsShow
      })

      if (recommendGoodsShow.length < that.data.recommendGoods.length) {
        that.setData({
          loadingMore: false,//关闭遮罩层
        })
      } else {
        that.setData({
          loadingMore: false,//关闭遮罩层
          isEnd: true
        })
      }
     
    } else if (rgShowLen < that.data.recommendGoods.length) {
      for (var i = rgShowLen; i < that.data.recommendGoods.length; i++) {
        recommendGoodsShow.push(that.data.recommendGoods[i])
      }
      //虚拟加载特效

      that.setData({
        recommendGoodsShow: recommendGoodsShow,
        loadingMore: false,//关闭遮罩层
        isEnd: true//显示没有了提示
      })

      
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     this.getRGshow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})